/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-09-14 19:47:12
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-09-18 22:02:11
 * @FilePath: \郭招娣\hou\app\controller\articleLists.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-09-12 10:40:26
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-09-16 22:12:08
 * @FilePath: \郭招娣\hou\app\controller\home.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
'use strict';

const Controller = require('egg').Controller;
class ArticleListController extends Controller {
    // 获取文章列表
 
    async articleLists() {
        const { ctx } = this;
        const { pageCode, pageSize, user, status } = ctx.request.body;
        // console.log(user,'11')
        // console.log(status, 'kkkka')

        let lists = await this.app.mysql.select('articleLists');
        console.log(lists.length)
       
        let newLists = lists;
      
        if (user) {
            newLists = newLists.filter(v => v.title.includes(user))
        }
        if(status){
            newLists = newLists.filter(v => v.state==status)
        }
        let total = newLists.length;
        newLists = newLists.slice((pageCode - 1) * pageSize, pageCode * pageSize)
        if (newLists.length) {
            ctx.body = {
                code: 200,
                total,
                results: newLists
            }
        } else {
            ctx.body = {
                code: 400,
                msg: '获取数据失败'
            }
        }
    }

    // 删除文章列表
    async deleteArticleLists() {
        const { ctx } = this;
        const { id } = ctx.query;
        const result = await this.app.mysql.delete('articleLists', { id });
        if (result) {
            const list = await this.app.mysql.select('articleLists');
            ctx.body = {
                code: 200,
                msg: '删除成功',
                list
            }
        } else {
            ctx.body = {
                code: 200,
                msg: '删除失败',
                list
            }
        }

    }
    // 添加文章
    async addArticleLists(){
        const { ctx }=this;
        const { title,  classes, tag  } = ctx.request.body;
        let newTag=JSON.stringify(tag)

        const result = await this.app.mysql.insert('articleLists', 
            { title, classes, state: 'false', readers: 0, likes: 0, date: new Date().toLocaleString('Chinese', { hour12: false }), tag:newTag });
        if(result){
            let list = await this.app.mysql.select('articleLists');
            ctx.body={
                code:200,
                msg:'添加成功',
                list
            }
        }else{
            ctx.body = {
                code: 500,
                msg: '添加失败',
            }
        }
    }


   async editArticleLists(){
    const {ctx }=this
       const { title, tag ,id} = ctx.request.body;
       let newTag = JSON.stringify(tag)
       const row = {
           title,
           state: 'false',    // any other fields u want to update
           readers:0, 
           likes:0,
           date: new Date().toLocaleString('Chinese', { hour12: false }), 
           tag: newTag
       };

       const options = {
           where: {
               id: id
           }
       };
    //    console.log(id,title,'ddfdf')
       const result = await this.app.mysql.update('articleLists', row, options);
       if(result){
           let list = await this.app.mysql.select('articleLists');
           ctx.body = {
               code: 200,
               msg: '编辑成功',
               list
           }
       } else {
           ctx.body = {
               code: 500,
               msg: '编辑失败',
           }
       }
   }

    // 多选删除
    async chooseDelete() {
        const { ctx } = this;
        const { multipleSelection, pageCode, pageSize } = ctx.request.body;
        const chooseLists = multipleSelection.map(v => {
            return this.app.mysql.delete('articleLists', { id: v.id })
        });

        if (chooseLists) {
            const list = await this.app.mysql.select('articleLists');
            if (list) {
                ctx.body = {
                    code: 200,
                    msg: '删除成功',
                    list: list.slice((pageCode - 1) * pageSize, pageCode * pageSize),
                    total: list.length
                }
            } else {
                ctx.body = {
                    code: 404,
                    msg: '删除失败',
                }
            }
        } else {
            ctx.body = {
                code: 404,
                msg: '多选删除错误',
            }
        }
    }

    // 发布状态
    async publish() {
        const { ctx } = this;
        const { multipleSelection } = ctx.request.body;
        const chooseLists = multipleSelection.map(v => {
            return this.app.mysql.update('articleLists', { id: v.id, state: 'true' })

        });

        if (chooseLists) {
            const list = await this.app.mysql.select('articleLists');
            if (list) {
                ctx.body = {
                    code: 200,
                    msg: '发布成功',
                    list,
                }
            } else {
                ctx.body = {
                    code: 404,
                    msg: '发布失败',
                }
            }
        } else {
            ctx.body = {
                code: 404,
                msg: '发布错误',
            }
        }


    }
    // 草稿
    async draft() {
        const { ctx } = this;
        const { multipleSelection } = ctx.request.body;
        const chooseLists = multipleSelection.map(v => {
            return this.app.mysql.update('articleLists', { id: v.id, state: 'false' })

        });

        if (chooseLists) {
            const list = await this.app.mysql.select('articleLists');
            if (list) {
                ctx.body = {
                    code: 200,
                    msg: '草稿成功',
                    list,
                }
            } else {
                ctx.body = {
                    code: 404,
                    msg: '草稿失败',
                }
            }
        } else {
            ctx.body = {
                code: 404,
                msg: '草稿错误',
            }
        }
    }

}

module.exports = ArticleListController;
